package io.framework.modules.air.controller;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.framework.common.utils.DateUtils;
import io.framework.common.utils.IQI;
import io.framework.common.utils.Result;
import io.framework.modules.air.dto.AirDayNumberDTO;
import io.framework.modules.air.service.AirDayNumberService;
import io.framework.modules.air.service.AirService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;


/**
 * 空气管理接口
 *
 * @author Gujq email
 * @since 1.0.0 2020-05-20
 */
@RestController
@Api(tags = "空气管理接口")
public class AirDataController {

    public static final String key = "air_monitor_info";

    @Autowired
    private AirService airService;

    @Autowired
    private AirDayNumberService airDayNumberService;

    @GetMapping("air/info/youLiangDayNumber")
    @ApiOperation("全市空气优良天数分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "1-同比 2-环比", paramType = "query", dataType = "String",required = true),
            @ApiImplicitParam(name = "date", value = "时间 2020-04", paramType = "query", dataType = "String",required = true)
    })
    public Result<List<Map<String,Object>>> youLiangNumber(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String,Object>> mapList = airDayNumberService.youLiangNumber(params);

        return new Result<List<Map<String,Object>>>().ok(mapList);
    }

    @GetMapping("air/info/youLiangDayNumberOfMonth")
    @ApiOperation("全市空气优良天数月度情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间 2020-04", paramType = "query", dataType = "String",required = true)
    })
    public Result<List<Map<String,Object>>> youLiangNumberMonth(@ApiIgnore @RequestParam Map<String, Object> params) {
        
        List<Map<String,Object>> mapList = airDayNumberService.youLiangNumberMonth(params);


        return new Result<List<Map<String,Object>>>().ok(mapList);
    }

    @GetMapping("air/info/aqiCalendar")
    @ApiOperation("全市AQI日历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "时间 2020-04", paramType = "query", dataType = "String",required = true),
    })
    public Result<List<Map<String,Object>>> aqiCalendar(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String,Object>> mapList = airService.aqiCalendar(params);
        for (Map<String,Object> map:mapList) {
            BigDecimal BAQI =  (BigDecimal)map.get("AQI");
            int AQI = BAQI.intValue();
            //    空气质量指数级别: 1: 一级（优）2: 二级（良）3: 三级（轻度污染） 4: 四级（中度污染） 5: 五级（重度污染） 6: 六级（严重污染）
            if(IQI.level(AQI)==1){
                map.put("level","一级");
            }else if(IQI.level(AQI)==2){
                map.put("level","二级");
            }else if(IQI.level(AQI)==3){
                map.put("level","三级");
            }else if(IQI.level(AQI)==4){
                map.put("level","四级");
            }else if(IQI.level(AQI)==5){
                map.put("level","五级");
            }else if(IQI.level(AQI)==6){
                map.put("level","六级");
            }
        }

        return new Result<List<Map<String,Object>>>().ok(mapList);
    }

    @GetMapping("air/info/aqiHistoryChange")
    @ApiOperation("站点AQI历史变化对比图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-国控 2-全市", paramType = "query", dataType = "int",required = true),
            @ApiImplicitParam(name = "stationId", value = "站点ID", paramType = "query", dataType = "array"),
            @ApiImplicitParam(name = "startDate", value = "开始时间 2020-5-20", paramType = "query", dataType = "String",required = true),
            @ApiImplicitParam(name = "endDate", value = "结束时间 2020-5-21", paramType = "query", dataType = "String",required = true),
    })
    public Result<List<Map<String,Object>>> aqiHistoryChange(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String,Object>> map =airService.aqiHistoryChange(params);

        return new Result<List<Map<String,Object>>>().ok(map);
    }

    @GetMapping("air/info/stationWYWndChange")
    @ApiOperation("站点污染物浓度变化")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1-国控 2-全市", paramType = "query", dataType = "int",required = true),
            @ApiImplicitParam(name = "stationId", value = "站点ID", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "startDate", value = "开始时间 2020-5-20", paramType = "query", dataType = "String",required = true),
            @ApiImplicitParam(name = "endDate", value = "结束时间 2020-5-21", paramType = "query", dataType = "String",required = true),
            @ApiImplicitParam(name = "factor", value = "因子 PM25 PM10 SO2 NO2 CO O3", paramType = "query", dataType = "String",required = true),
    })
    public Result<List<Map<String,Object>>> stationWYWndChange(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String,Object>> mapList =airService.factorHistoryChange(params);

        return new Result<List<Map<String,Object>>>().ok(mapList);
    }


}